package com.newangels.filter;

import com.newangels.service.AgentManagerService;
import com.newangels.service.impl.AgentManagerServiceImpl;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * 新增修改Agent时 校验路径 校验编码是否重复
 *
 * @author: TangLiang
 * @date: 2020/5/16 17:21
 * @since: 1.0
 */
@WebFilter(urlPatterns = {"/insertAgent", "/updateAgent"})
public class AgentUrlFilter implements Filter {
    AgentManagerService agentManagerService = AgentManagerServiceImpl.getAgentManagerServiceImpl();

    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        String AGENT_URL_ = request.getParameter("AGENT_URL_");
        String fileUrl = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        AGENT_URL_ = AGENT_URL_.replace(".", File.separator) + ".java";
        //项目名
        String path = request.getServletContext().getContextPath();
        if ("usr".equals(fileUrl.substring(1, 4))) {
            fileUrl = (fileUrl.substring(0, fileUrl.length() - 16));//linux
            //截取 项目根路径
            int index = fileUrl.indexOf(path);
            //生成新的路径
            fileUrl = fileUrl.substring(0, index) + path + File.separator + "src" + File.separator;
        } else {
            fileUrl = (fileUrl.substring(1, fileUrl.length() - 16));//windows
            //截取 项目根路径
            int index = fileUrl.indexOf(path);
            //生成新的路径
            fileUrl = fileUrl.substring(0, index) + path + File.separator + "src" + File.separator;
        }
        //路径不存在返回错误信息,存在则校验编码是否重复
        File file = new File(fileUrl, AGENT_URL_);
        if (!file.exists()) { //路径不存在返回错误信息
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/plain");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("success", false);
            jsonObject.put("message", "Agent路径不存在");
            response.getWriter().write(jsonObject.toString());
        } else {
            //路径存在校验编码是否存在
            String AGENT_CODE_ = request.getParameter("AGENT_CODE_");
            String AGENT_ID_ = request.getParameter("AGENT_ID_");
            if (StringUtils.isNotEmpty(AGENT_ID_)) {  //修改Agent  先判断是否修改了AGENT_CODE_ 如果修改了再判断AGENT_CODE_ 是否存在
                if (agentManagerService.getcount(AGENT_ID_, AGENT_CODE_, null, null, null, null, null, null, null) == 0) {
                    if (agentManagerService.getcount(null, AGENT_CODE_, null, null, null, null, null, null, null) != 0) {
                        request.setCharacterEncoding("UTF-8");
                        response.setCharacterEncoding("UTF-8");
                        response.setContentType("text/plain");
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("success", false);
                        jsonObject.put("message", "Agent编码已存在");
                        response.getWriter().write(jsonObject.toString());
                    } else {
                        chain.doFilter(req, resp);
                    }
                } else {
                    chain.doFilter(req, resp);
                }
            } else { //新增Agent
                if (agentManagerService.getcount(null, AGENT_CODE_, null, null, null, null, null, null, null) != 0) {
                    request.setCharacterEncoding("UTF-8");
                    response.setCharacterEncoding("UTF-8");
                    response.setContentType("text/plain");
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("success", false);
                    jsonObject.put("message", "Agent编码已存在");
                    response.getWriter().write(jsonObject.toString());
                } else {
                    chain.doFilter(req, resp);
                }
            }
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
